---
import type { TocItem } from '@astrojs/starlight/utils/generateToC'

interface Props {
  toc: TocItem[]
  depth?: number
  isMobile?: boolean
}

const { toc, isMobile = false, depth = 0 } = Astro.props
---

<ul class:list={{ isMobile }}>
  {
    toc.map(heading => (
      <li>
        <a href={'#' + heading.slug} style={`padding-left: ${depth * 8}px;`}>
          <span>{heading.text}</span>
        </a>
        {heading.children.length > 0 && (
          <Astro.self
            toc={heading.children}
            depth={depth + 1}
            isMobile={isMobile}
          />
        )}
      </li>
    ))
  }
</ul>

<style define:vars={{ depth }}>
  ul {
    padding: 0;
    list-style: none;
    row-gap: 4px;
  }
  a {
    color: var(--secondary-text-color);
    font-family: 'Inter', sans-serif;
    font-size: 0.8125rem;
    font-weight: 500;
    letter-spacing: -0.015rem;
    margin-bottom: 8px;
    transition: color 0.1s ease-in-out;
    &:hover {
      text-decoration: underline;
      color: var(--primary-text-color) !important;
      text-decoration-color: var(--hover-color) !important;
      text-underline-offset: 3.2px;
    }
  }
  a[aria-current='true'] {
    color: var(--primary-text-color) !important;
    :before {
      content: '_';
    }
  }
  .isMobile a {
    --pad-inline: 1rem;
    display: flex;
    justify-content: space-between;
    gap: var(--pad-inline);
    border-top: var(--border);
    border-radius: 0;
    padding-block: 0.5rem;
    text-decoration: none;
    outline-offset: var(--sl-outline-offset-inside);
  }
  .isMobile:first-child > li:first-child > a {
    border-top: 0;
  }
  .isMobile a[aria-current='true'],
  .isMobile a[aria-current='true']:hover,
  .isMobile a[aria-current='true']:focus {
    color: var(--primary-text-color);
    background-color: unset;
  }
  .isMobile a[aria-current='true']::after {
    content: '';
    width: 1rem;
    /* Check mark SVG icon */
    -webkit-mask-image: url('');
    mask-image: url('');
    -webkit-mask-repeat: no-repeat;
    mask-repeat: no-repeat;
    flex-shrink: 0;
  }
</style>
